package com.xnccc.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.owen.mapx.MapX;
import com.xnccc.audioset.MainActivity;
import com.xnccc.audioset.R;
import com.xnccc.view.VerticalSeekBar;
import com.xnccc.view.XNCCCDialog;
import com.xnccc.view.XNCCCDialog.XNCCCDialogListItemListener;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class DelayFragment extends Fragment implements OnClickListener {

	TextView[] showTxts = new TextView[7];
	VerticalSeekBar seekBars[] = new VerticalSeekBar[7];
	private TextView mm1, mm2, mm3, mm4, mm5, mm6, m1, m2, m3, m4, m5, m6;
	private ArrayList<Map<String, String>> time_data;
	private MapX bird;
	private SharedPreferences sp = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		bird = MainActivity.bird;
		View mView = inflater.inflate(R.layout.fragment_delay, container, false);
		initView(mView);
		return mView;
	}

	private void initView(View mView) {
		init();
		mView.findViewById(R.id.dl_m1).setOnClickListener(this);
		mView.findViewById(R.id.dl_m2).setOnClickListener(this);
		mView.findViewById(R.id.dl_m3).setOnClickListener(this);
		mView.findViewById(R.id.dl_m4).setOnClickListener(this);
		mView.findViewById(R.id.dl_m5).setOnClickListener(this);
		mView.findViewById(R.id.dl_m6).setOnClickListener(this);

		mView.findViewById(R.id.dl_mm1).setOnClickListener(this);
		mView.findViewById(R.id.dl_mm2).setOnClickListener(this);
		mView.findViewById(R.id.dl_mm3).setOnClickListener(this);
		mView.findViewById(R.id.dl_mm4).setOnClickListener(this);
		mView.findViewById(R.id.dl_mm5).setOnClickListener(this);
		mView.findViewById(R.id.dl_mm6).setOnClickListener(this);

		mm1 = (TextView) mView.findViewById(R.id.txt_mm1);
		mm2 = (TextView) mView.findViewById(R.id.txt_mm2);
		mm3 = (TextView) mView.findViewById(R.id.txt_mm3);
		mm4 = (TextView) mView.findViewById(R.id.txt_mm4);
		mm5 = (TextView) mView.findViewById(R.id.txt_mm5);
		mm6 = (TextView) mView.findViewById(R.id.txt_mm6);

		m1 = (TextView) mView.findViewById(R.id.txt_m1);
		m2 = (TextView) mView.findViewById(R.id.txt_m2);
		m3 = (TextView) mView.findViewById(R.id.txt_m3);
		m4 = (TextView) mView.findViewById(R.id.txt_m4);
		m5 = (TextView) mView.findViewById(R.id.txt_m5);
		m6 = (TextView) mView.findViewById(R.id.txt_m6);
		sp = getActivity().getSharedPreferences("xn_sp",Context.MODE_PRIVATE);
		
		m2.setText(sp.getInt("l1", 0)+"");
		mm2.setText(sp.getInt("l2", 0)+"");
		m1.setText(sp.getInt("c1", 0)+"");
		mm1.setText(sp.getInt("c2", 0)+"");
		m3.setText(sp.getInt("r1", 0)+"");
		mm3.setText(sp.getInt("r2", 0)+"");
		m4.setText(sp.getInt("sr1", 0)+"");
		mm4.setText(sp.getInt("sr2", 0)+"");
		m5.setText(sp.getInt("sl1", 0)+"");
		mm5.setText(sp.getInt("sl2", 0)+"");
		m6.setText(sp.getInt("lfe1", 0)+"");
		mm6.setText(sp.getInt("lfe2", 0)+"");
		mView.findViewById(R.id.saveDl).setOnClickListener(this);
	}

	private void init() {
		time_data = new ArrayList<Map<String, String>>();
		for (int i = 0; i < 10; i++) {
			Map<String, String> map_pb = new HashMap<String, String>();
			map_pb.put("id", i + "");
			map_pb.put("text", i + "");
			time_data.add(map_pb);
		}
	}

	public void showD(final TextView view) {
		XNCCCDialog.showListDialog(getActivity(), time_data, 0, new XNCCCDialogListItemListener() {

			@Override
			public void OnChoseListener(Dialog dialog, Map<String, String> item) {
				view.setText(item.get("text"));
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.dl_m1:
			showD(m1);
			break;
		case R.id.dl_m2:
			showD(m2);
			break;
		case R.id.dl_m3:
			showD(m3);
			break;
		case R.id.dl_m4:
			showD(m4);
			break;
		case R.id.dl_m5:
			showD(m5);
			break;
		case R.id.dl_m6:
			showD(m6);
			break;
		case R.id.dl_mm1:
			showD(mm1);
			break;
		case R.id.dl_mm2:
			showD(mm2);
			break;
		case R.id.dl_mm3:
			showD(mm3);
			break;
		case R.id.dl_mm4:
			showD(mm4);
			break;
		case R.id.dl_mm5:
			showD(mm5);
			break;
		case R.id.dl_mm6:
			showD(mm6);
			break;
		case R.id.saveDl:
			if ((getTNum(mm1).equals("0") && getTNum(m1).equals("0"))
					|| (getTNum(mm2).equals("0") && getTNum(m2).equals("0"))
					|| (getTNum(mm3).equals("0") && getTNum(m3).equals("0"))
					|| (getTNum(mm4).equals("0") && getTNum(m4).equals("0"))
					|| (getTNum(mm5).equals("0") && getTNum(m5).equals("0"))
					|| (getTNum(mm6).equals("0") && getTNum(m6).equals("0"))) {
				Toast.makeText(getActivity(), "每一组均不能为空", Toast.LENGTH_SHORT).show();
			} else {
				getLowNum();
				Toast.makeText(getActivity(), "成功设置", Toast.LENGTH_SHORT).show();
			}

			break;
		}
	}

	private String getTNum(TextView view) {
		return view.getText().toString();
	}

	private int getINum(TextView view) {
		return Integer.parseInt(view.getText().toString());
	}

	private void getLowNum() {

		double[] nums = { getINum(m1) + getINum(mm1) / 10.0, getINum(m2) + getINum(mm2) / 10.0,
				getINum(m3) + getINum(mm3) / 10.0, getINum(m4) + getINum(mm4) / 10.0, getINum(m5) + getINum(mm5) / 10.0,
				getINum(m6) + getINum(mm6) / 10.0 };

		double lNum = nums[0];
		for (int i = 0; i < nums.length; i++) {
			Log.e("arr", nums[i] + "");
			if (lNum > nums[i]) {
				lNum = nums[i];
			}
		}

		Editor edit = sp.edit();
		edit.putInt("l1", getINum(m2));
		edit.putInt("l2", getINum(mm2));
		edit.putInt("c1", getINum(m1));
		edit.putInt("c2", getINum(mm1));
		edit.putInt("r1", getINum(m3));
		edit.putInt("r2", getINum(mm3));
		edit.putInt("sr1", getINum(m4));
		edit.putInt("sr2", getINum(mm4));
		edit.putInt("sl1", getINum(m5));
		edit.putInt("sl2", getINum(mm5));
		edit.putInt("lfe1", getINum(m6));
		edit.putInt("lfe2", getINum(mm6));
		edit.commit();

		bird.setDelay(MapX.CH_C, (int) ((nums[0] - lNum) / 0.34));
		bird.setDelay(MapX.CH_R, (int) ((nums[1] - lNum) / 0.34));
		bird.setDelay(MapX.CH_L, (int) ((nums[2] - lNum) / 0.34));
		bird.setDelay(MapX.CH_SL, (int) ((nums[3] - lNum) / 0.34));
		bird.setDelay(MapX.CH_SR, (int) ((nums[4] - lNum) / 0.34));
		bird.setDelay(MapX.CH_LFE, (int) ((nums[5] - lNum) / 0.34));
	}

}
